package com.luoyuanxiangvip.common.utils;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 获取系统信息
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019/6/27
 */
@Slf4j
public class UaUtils {
    /**
     * 根据userAgent解析出osVersion
     * @param userAgent userAgent
     * @return osVersion
     */
    public static String getOsVersion(String userAgent) {
        String osVersion = "";
        if(StringUtils.isBlank(userAgent)) {
            return osVersion;
        }
        String[] strArr = userAgent.substring(userAgent.indexOf("(")+1,
                userAgent.indexOf(")")).split(";");
        if(strArr.length == 0) {
            return osVersion;
        }
        osVersion = strArr[1];
        log.info("osVersion -> {}", osVersion);
        return osVersion;
    }

    /**
     * 获取操作系统对象
     * @param userAgent userAgent
     * @return OperatingSystem
     */
    private static OperatingSystem getOperatingSystem(String userAgent) {
        UserAgent agent = UserAgent.parseUserAgentString(userAgent);
        return agent.getOperatingSystem();
    }

    /**
     * 获取os：Windows/ios/Android
     * @param userAgent userAgent
     * @return Windows/ios/Android
     */
    public static String getOs(String userAgent) {
        OperatingSystem operatingSystem =  getOperatingSystem(userAgent);
        String os = operatingSystem.getGroup().getName();
        log.info("os -> {}", os);
        return os;
    }

    /**
     * 获取deviceType
     * @param userAgent userAgent
     * @return deviceType
     */
    public static String getDevicetype(String userAgent) {
        OperatingSystem operatingSystem =  getOperatingSystem(userAgent);
        String deviceType = operatingSystem.getDeviceType().toString();
        log.info("deviceType -> {}", deviceType);
        return deviceType;
    }

    /**
     * 获取操作系统的名字
     * @param userAgent userAgent
     * @return String
     */
    public static String getOsName(String userAgent) {
        OperatingSystem operatingSystem =  getOperatingSystem(userAgent);
        String osName = operatingSystem.getName();
        log.info("操作系统的名字 -> {}", osName);
        return osName;
    }

    /**
     * 获取device的生产厂家
     * @param userAgent userAgent
     * @return String
     */
    public static String getDeviceManufacturer(String userAgent) {
        OperatingSystem operatingSystem =  getOperatingSystem(userAgent);
        String deviceManufacturer = operatingSystem.getManufacturer().toString();
        log.info("device的生产厂家 -> {}", deviceManufacturer);
        return deviceManufacturer;
    }

    /**
     * 获取浏览器对象
     * @param agent agent
     * @return Browser
     */
    public static Browser getBrowser(String agent) {
        UserAgent userAgent = UserAgent.parseUserAgentString(agent);
        return userAgent.getBrowser();
    }

    /**
     * 获取browser name
     * @param userAgent userAgent
     * @return String
     */
    public static String getBorderName(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        String borderName = browser.getName();
        log.info("borderName -> {}", borderName);
        return borderName;
    }

    /**
     * 获取浏览器的类型
     * @param userAgent userAgent
     * @return String
     */
    public static String getBorderType(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        String borderType = browser.getBrowserType().getName();
        log.info("浏览器的类型 -> {}", borderType);
        return borderType;
    }

    /**
     * 获取浏览器组： CHROME、IE
     * @param userAgent userAgent
     * @return CHROME、IE
     */
    public static String getBorderGroup(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        String browerGroup = browser.getGroup().getName();
        log.info("浏览器组 -> {}", browerGroup);
        return browerGroup;
    }


    /**
     * 获取浏览器的生产厂商
     * @param userAgent userAgent
     * @return String
     */
    public static String getBrowserManufacturer(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        String browserManufacturer = browser.getManufacturer().getName();
        log.info("浏览器的生产厂商 -> {}", browserManufacturer);
        return browserManufacturer;
    }

    /**
     * 获取浏览器使用的渲染引擎
     * @param userAgent userAgent
     * @return String
     */
    public static String getBorderRenderingEngine(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        String renderingEngine = browser.getRenderingEngine().name();
        log.info("浏览器使用的渲染引擎 -> {}", renderingEngine);
        return renderingEngine;
    }

    /**
     * 获取浏览器版本
     * @param userAgent userAgent
     * @return String
     */
    public static String getBrowserVersion(String userAgent) {
        Browser browser =  getBrowser(userAgent);
        log.info("浏览器版本 -> {}",browser.getVersion(userAgent));
        return browser.getVersion(userAgent).toString();
    }


    public static void main(String[] args) {
        String androidUserAgent = "Mozilla/5.0 (Linux; Android 8.0; LON-AL00 Build/HUAWEILON-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044204 Mobile Safari/537.36 V1_AND_SQ_7.7.8_908_YYB_D QQ/7.7.8.3705 NetType/WIFI WebP/0.3.0 Pixel/1440";
        String iosUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A366 QQ/7.7.8.421 V1_IPH_SQ_7.7.8_1_APP_A Pixel/750 Core/UIWebView Device/Apple(iPhone 6s) NetType/WIFI QBWebViewType/1";
        String winUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";

        System.out.println("浏览器组："+getBorderGroup(androidUserAgent));
        System.out.println("浏览器名字："+getBorderName(androidUserAgent));
        System.out.println("浏览器类型"+getBorderType(androidUserAgent));
        System.out.println("浏览器生产商："+getBrowserManufacturer(androidUserAgent));
        System.out.println("浏览器版本："+getBrowserVersion(androidUserAgent));
        System.out.println("设备生产厂商:"+getDeviceManufacturer(androidUserAgent));
        System.out.println("设备类型:"+getDevicetype(androidUserAgent));
        System.out.println("设备操作系统："+getOs(androidUserAgent));
        System.out.println("操作系统的名字："+getOsName(androidUserAgent));
        System.out.println("操作系统的版本号："+getOsVersion(androidUserAgent));
        System.out.println("操作系统浏览器的渲染引擎:"+getBorderRenderingEngine(androidUserAgent));

    }
}
